$(function () {
    function find(url,page,rows,classId,name,author) {
        $.get(url,
            {page:page,rows:rows,classId:classId,name:name,author:author},
            function (data) {
               console.log(data.data,1111)
                if(data.data){
                    let date =""
                    for(let i of data.data.list) date+=  `<tr>
                                                <td>${i.id}</td>
                                               <td>${i.classId}</td>
                                                <td>${i.name}</td>
                                                <td>${i.author}</td>
                                                <td style="white-space: nowrap;
                                                            text-overflow: ellipsis;
                                                            overflow: hidden;
                                                            max-width: 200px;">
                                                 ${i.description}</td>
                                                <td>${i.chapterCount}</td>
                                                <td>
                                                    <a  href="javascript:void(0)"data-edit-desc=${i.description}  data-edit-classId=${i.classId} data-edit-name="${i.name}" data-edit-chap=${i.chapterCount} data-edit-author=${i.author} data-edit-id=${i.id}>修改</a>
                                                    <a href="javascript:void(0)" data-del-id=${i.id}>删除</a>
                                                </td>
                                            </tr>`
                    $('#data-tab tbody').empty().append(date)
                    let pageString = '';
                    if(data.data.page > 1){ // 当前页
                        pageString += `<li><a href="javascript:void(0)" data-page="${data.data.page-1}">&laquo;</a></li>`;
                    }
                    for(let i=data.data.page;i < data.data.page+3 && i<data.data.pages; i++){
                        pageString += `
                        <li class="${i===data.data.page?'active':''}">
                        <a href="javascript:void(0)" data-page="${i}">${i}</a></li>`;
                    }
                    if(data.data.page  < data.data.pages){ // 当前页
                        pageString += `<li><a href="javascript:void(0)" data-page="${data.data.page + 3}">&raquo;</a></li>`;
                    }
                    console.log(pageString,11111234213)
                    $("#data-tab-page").empty().html(pageString);
                    $("#data-tab-page li a").click(function(){
                        console.log(data.data.pages,typeof data.pages)
                        let page = parseInt($(this).attr("data-page"));
                        let pages = data.data.pages
                        let classId = $("[name='classId']").val()
                        let className = $("[name='className']").val()
                        if(page>0||page<pages) find("/novel",page,2,classId,className);
                    });
                    $("[data-del-id]").click(function () {
                        console.log(11111)
                        let id = $(this).attr("data-del-id")
                        $.ajax({
                                url:"/novel?id="+id,
                                type: "DELETE",
                                success: function (data) {
                                    console.log(data)
                                    let classId = $("[name='classId']").val()
                                    let name = $("[name='name']").val()
                                    let author = $("[name='author']").val()
                                    find("/novel",1,2,classId,name,author)
                                },
                                error: function (data) {
                                    console.log(data)
                                }
                            }

                        )
                    })
                    $("[data-edit-id]").click(function () {
                        console.log(111)
                        sessionStorage.setItem("id",$(this).attr("data-edit-id"))
                        sessionStorage.setItem("name",$(this).attr("data-edit-name"))
                        sessionStorage.setItem("classId",$(this).attr("data-edit-classId"))
                        sessionStorage.setItem("author",$(this).attr("data-edit-author"))
                        sessionStorage.setItem("desc",$(this).attr("data-edit-desc"))
                        sessionStorage.setItem("chap",$(this).attr("data-edit-chap"))
                        location.href="edit.html"
                    })
                    $('.pub-novel').click(function () {
                        $.ajax({
                            url:"/classes",
                            type:"GET",
                            success:function (data) {
                                localStorage.setItem("types",JSON.stringify(data.types))
                                location.href= 'add.html'
                            }
                        })

                    })
                }
            })
    }
    let userName = sessionStorage.getItem('userName')
    if(userName!=null) find("/novel",1,2)
    else location.href = '/pages/user/error.html'
    $('#search').click(function () {
        console.log(1)
        let classId = $("[name='classId']").val()
        let name = $("[name='name']").val()
        let author = $("[name='author']").val()
        find("/novel",1,2,classId,name,author)
    })

})